package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 合同信息Service业务层处理
 *
 * @author qqq
 * @date 2024-12-28
 */
@Service
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同信息
     *
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contract selectContractById(Long id) {
        return getById(id);
    }

    /**
     * 查询合同信息列表
     *
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同信息
     *
     * @param contract 合同信息
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract) {
        return save(contract) ? 1 : 0;
    }

    /**
     * 修改合同信息
     *
     * @param contract 合同信息
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract) {
        return updateById(contract) ? 1 : 0;
    }

    /**
     * 批量删除合同信息
     *
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除合同信息信息
     *
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 更新合同状态任务
     */
    @Override
    public void updateContractStatus() {
        //1.查询合同状态为0和1的数据
        List<Contract> contractList = lambdaQuery().in(Contract::getStatus, List.of(0, 1)).list();

        log.info("查询到需要更新状态的合同数量为：{}", contractList.size());
        if (CollUtil.isNotEmpty(contractList)) {
            List<Contract> updateContractList = new ArrayList<>();
            LocalDateTime now = LocalDateTime.now();
            for (Contract contract : contractList) {
                if (ObjectUtil.equal(contract.getStatus(), 0)) {

                    if (!contract.getStartDate().isAfter(now)) {
                        //判断结束时间
                        if (contract.getEndDate().isAfter(now)) {
                            updateContractList.add(buildUpdateContract(contract, 1));
                        } else {
                            updateContractList.add(buildUpdateContract(contract, 2));
                        }
                    }
                } else {
                    if (!contract.getEndDate().isAfter(now)) {
                        updateContractList.add(buildUpdateContract(contract, 3));
                    }
                }
            }
            //4. 批量更新
            log.info("此次需要更新状态的合同有:{}", updateContractList.size());
            if (CollUtil.isNotEmpty(updateContractList)) {
                updateBatchById(updateContractList);
            }
        }
    }

    private Contract buildUpdateContract(Contract contract, Integer status) {
        Contract updateContract = new Contract();
        updateContract.setId(contract.getId());
        updateContract.setStatus(status);
        return updateContract;
    }
}
